import { Component, OnInit } from '@angular/core';
import { NzTreeNode,NzModalService } from 'ng-zorro-antd';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
@Component({
  selector: 'app-staff',
  templateUrl: './staff.component.html',
  styleUrls: ['./staff.component.css'],
  styles: [ `
  ::ng-deep .vertical-center-modal {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  ::ng-deep .vertical-center-modal .ant-modal {
    top: 0;
  }
  ::ng-deep .iconEdit{
    font-size: 16px;
    color:#4395ff;
    cursor: pointer;
  }
  ::ng-deep .iconDel{
    font-size: 16px;
    color:#4395ff;
    cursor: pointer;
}
::ng-deep .marginTop{
  margin-top:10px;
}
::ng-deep .addStaff{
  margin-left:20px;
}
` ]
})
export class staffComponent implements OnInit { 
  constructor(private http: HttpClient, private modalService: NzModalService) {
  }
  cityData:any=[];
  staffAge:any;
  staffPhone:any;
  staffTime:any;
  staffPosition:any;
  staffStatus:any;
  staffCity:any;
  staffSex:any;
  staffName:any;
  isAdd = 0;
  isVisible = false;
  staffAdd=false;
  dataSet = [];
  allDataSet = [];
  staffId:any;
  mouseAction(name: string, e: any): void {
    console.log(name, e);
  }
  ngOnInit() {
    this.http.get('http://www.zhengbenzixun.cn:8080/cityCount').subscribe(res => {
      for(let i in res){
        this.cityData.push({
          cityName:res[i].cityName
        })
      }
    })
    this.http.get('http://www.zhengbenzixun.cn:8080/staffCount').subscribe(res => {
      for(let i in res){
        res[i].expand = false;
        res[i].statusType = 'finish';
        this.dataSet = [...this.dataSet, res[i]];
      }
    })
  }
  edit(e){
    this.isAdd = 1;
    this.staffAdd = true;
    this.staffAge=e.staffAge;
    this.staffPhone=e.staffPhone;
    this.staffTime=e.staffTime;
    this.staffPosition=e.staffPosition;
    this.staffStatus=e.staffStatus;
    this.staffCity=e.staffCity;
    this.staffSex=e.staffSex;
    this.staffName=e.staffName;
    this.staffId=e._id
  }
  del(e){
    this.isVisible = true;
    this.staffName=e.staffName;
    this.staffId = e._id
  }
  delYes(){
    let params = new URLSearchParams();
      params.append("staffId", this.staffId);
      let body = params.toString();
      let header = {
        headers: new HttpHeaders({
          'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
        })
      };
    this.http.post('http://www.zhengbenzixun.cn:8080/delStaff',body, header).subscribe(res => {
      this.isVisible = false;
      const modal = this.modalService.success({
        nzTitle: '删除成功',
        nzContent: '已成功删除员工'+this.staffName
      });
      window.setTimeout(() => modal.destroy(), 1000);
      this.allDataSet = [];
          for (let i in res) {
            this.allDataSet.push(res[i]);
          }
          this.dataSet = [...this.allDataSet];
    })
  }
  delNo(): void {
    this.isVisible = false;
  }
  addStaff(){
    this.isAdd = 0;
    this.staffAdd = true;
    this.staffAge=null;
    this.staffPhone=null;
    this.staffTime=null;
    this.staffPosition=null;
    this.staffStatus=null;
    this.staffCity=null;
    this.staffSex=null;
    this.staffName=null;
  }
  addSave():void{
    if(this.isAdd == 0){
    if (this.staffAge == undefined || this.staffPhone == undefined || this.staffTime == undefined || this.staffPosition == undefined || this.staffStatus == undefined
    || this.staffCity == undefined || this.staffSex==undefined || this.staffName==undefined
    ) {
      this.error();
    } else if(this.staffPhone.length != 11){
      const modal = this.modalService.error({
        nzTitle: '电话信息错误',
        nzContent: '电话长度须为11位'
      });
      window.setTimeout(() => modal.destroy(), 1000);
    } else {
      let params = new URLSearchParams();
      params.append("staffName", this.staffName);
      params.append("staffSex", this.staffSex);
      params.append("staffAge", this.staffAge);
      params.append("staffPhone", this.staffPhone);
      params.append("staffTime", this.staffTime);
      params.append("staffPosition", this.staffPosition);
      params.append("staffStatus", this.staffStatus);
      params.append("staffCity", this.staffCity);
      let body = params.toString();
      let header = {
        headers: new HttpHeaders({
          'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
        })
      };
      this.http.post('http://www.zhengbenzixun.cn:8080/staffAdd', body, header).subscribe(res => {
        this.staffAdd = false;
        const modal = this.modalService.success({
          nzTitle: '新增成功',
          nzContent: '已成功添加一名员工'
        });
        window.setTimeout(() => modal.destroy(), 1000);
        this.allDataSet = [];
        for (let i in res) {
          this.allDataSet.push(res[i]);
        }
        this.dataSet = [...this.allDataSet];
      })
    }
  }else{
    if (this.staffAge == undefined || this.staffPhone == undefined || this.staffTime == undefined || this.staffPosition == undefined || this.staffStatus == undefined
      || this.staffCity == undefined || this.staffSex==undefined || this.staffName==undefined
      ) {
        this.error();
      } else if(this.staffPhone.length != 11){
        const modal = this.modalService.error({
          nzTitle: '电话信息错误',
          nzContent: '电话长度须为11位'
        });
        window.setTimeout(() => modal.destroy(), 1000);
      } else {
    let params = new URLSearchParams();
    params.append("staffName", this.staffName);
    params.append("staffSex", this.staffSex);
    params.append("staffAge", this.staffAge);
    params.append("staffPhone", this.staffPhone);
    params.append("staffTime", this.staffTime);
    params.append("staffPosition", this.staffPosition);
    params.append("staffStatus", this.staffStatus);
    params.append("staffCity", this.staffCity);
    params.append("staffId", this.staffId);
    let body = params.toString();
    let header = {
      headers: new HttpHeaders({
        'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
      })
    };
    this.http.post('http://www.zhengbenzixun.cn:8080/staffEdit', body, header).subscribe(res => {
      this.staffAdd = false;
      const modal = this.modalService.success({
        nzTitle: '修改成功',
        nzContent: '已成功修改一名员工'
      });
      window.setTimeout(() => modal.destroy(), 1000);
      this.allDataSet = [];
      for (let i in res) {
        this.allDataSet.push(res[i]);
      }
      this.dataSet = [...this.allDataSet];
    })
  }
  }
  }
  error(): void {
    const modal = this.modalService.error({
      nzTitle: '信息不完整',
      nzContent: '请将所有信息填写完整'
    });
    window.setTimeout(() => modal.destroy(), 1000);
  }
  addcalcel():void{
    this.staffAdd = false;
    this.staffAge=null;
    this.staffPhone=null;
    this.staffTime=null;
    this.staffPosition=null;
    this.staffStatus=null;
    this.staffCity=null;
    this.staffSex=null;
    this.staffName=null;
  }
}
